package com.jt.web.controller;


import com.fasterxml.jackson.databind.util.JSONPObject;
import com.jt.pojo.ItemDesc;
import com.jt.util.ObjectMapperUtil;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class JSONPController {
    /**
     * 完成JSONP的跨域访问
     * url: http://manage.jt.com/web/testJSONP?callback=jQuery11110866571904203129_1607672014673&_=1607672014674
     * 参数:callback=jQuxxxxxx
     * 返回值:callback(JSON)
     */

    public String jsonp(String callback) {
        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(101L).setItemDesc("跨域访问");
        String json = ObjectMapperUtil.toJSON(itemDesc);
        return callback + "(" + json + ")";
    }

    /**
     * 冠以JSONPObject 对象说明
     * */
    @RequestMapping("/web/testJSONP")
    public JSONPObject jsonp2(String callback){
        ItemDesc itemDesc = new ItemDesc();
        itemDesc.setItemId(101L).setItemDesc("跨域访问");
        return new JSONPObject(callback,itemDesc);
    }




}
